package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.Menu;
import org.zsk.cs.domain.po.Role;
import org.zsk.cs.domain.vo.MenuVo;
import org.zsk.cs.domain.vo.RoleVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.MenuService;
import org.zsk.fastdev.assign.Assign;
import org.zsk.fastdev.bean.BeanUtils;

import java.util.List;

/**
 * <p>
 * 菜单 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    private final MenuService menuService;
    private final Assign assign;

    @Autowired
    public MenuController(MenuService menuService, Assign assign) {
        this.menuService = menuService;
        this.assign = assign;
    }

    @PostMapping("/a")
    public ResultBody add(@RequestBody MenuVo menuVo) throws Exception {
        Menu menu=new Menu();
        assign.assign(menuVo,menu);
        menuService.save(menu);
        return ResultBody.success();
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody MenuVo menuVo) throws Exception {
        Menu menu=new Menu();
        assign.assign(menuVo,menu);
        menuService.updateById(menu);
        return ResultBody.success();
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        menuService.removeByIds(ids);
        return ResultBody.success();
    }

    @GetMapping("/g")
    public ResultBody page(MenuVo menuVo) throws Exception {
        Page<Menu> page=new Page<>();
        assign.assign(menuVo,page,"$current,$size");
        return ResultBody.success(menuService.page(page));
    }

    @GetMapping("/search")
    public ResultBody search(MenuVo menuVo) throws Exception {
        return ResultBody.success(menuService.search(menuVo));
    }

    @GetMapping("/gall")
    public ResultBody getAll() throws Exception {
        return ResultBody.success(menuService.list());
    }

}
